package com.baizhi.controller;

import com.baizhi.entity.Orders;
import com.baizhi.entity.ProductOrder;
import com.baizhi.entity.Result;
import com.baizhi.entity.UserOrders;
import com.baizhi.service.OrdersService;
import com.baizhi.service.ProductOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("orders")
public class OrdersController {

    @Autowired
    private OrdersService ordersService;

    @Autowired
    private ProductOrderService productOrderService;

    @RequestMapping("findAll")
    @ResponseBody
    public List<UserOrders> findAll(){
        return ordersService.showAll();
    }

    @RequestMapping("save")

    public Result save(ProductOrder productOrder,Orders orders){
        Result result = null;
        try{
            ordersService.save(orders);
            productOrder.setOrderid(orders.getId());
            productOrderService.save(productOrder);
        }catch (Exception e){
            e.printStackTrace();
            result = new Result();
            result.setMessage("添加失败");
        }
        return  result;
    }

    @RequestMapping("findById")
    @ResponseBody
    public UserOrders findById(String orderid,String productorderid){
        UserOrders byId = ordersService.findById(orderid, productorderid);
        return byId;
    }

    @RequestMapping("update")
    @ResponseBody
    public Result update(Orders orders,String orderid){
        Result result = null;
        try{
            ordersService.update(orders,orderid);
            result = new Result();
            result.setStatus(true);
            result.setMessage("更新成功");
        }catch (Exception e){
            e.printStackTrace();
            result = new Result();
            result.setMessage("更新失败");
        }
        return result;
    }

    @RequestMapping("delete")
    @ResponseBody
    public Result delete(String orderid){
        Result result = null;
        try{
            ordersService.delete(orderid);
            result = new Result();
            result.setStatus(true);
            result.setMessage("删除成功");
        }catch (Exception e){
            e.printStackTrace();
            result = new Result();
            result.setMessage("删除失败");
        }
        return result;
    }
}
